using System;
using System.Collections.Generic;
using System.Text;
using NUnit.Framework;
using Fieldbook.Entities;
using Fieldbook.DAL;
using System.Diagnostics;

namespace Fieldbook.NUnitTests.Fieldbook.DAL
{
    [TestFixture]
    public class MessageTestDAO : DAOTest
    {
        [Test]
        public void AddMessageTest()
        {
            Exception exc = null;
            Message message = null;
            try
            {
                message = GetTestMessage();
                Appuser user = GetTestUser();
                AppuserDAO.InsertAppuser(user);
                message.Senderid = user.Userid;
                message.Receiverid = user.Userid;
                MessageDAO.InsertMessage(message);
                Assert.IsNotNull(message.Messageid);
                MessageDAO.DeleteMessage(message);
                AppuserDAO.DeleteAppuser(user);
            }
            catch (Exception ex)
            {
                Trace.Write(ex);
                exc = ex;
            }
            Assert.IsNotNull(message);
            Assert.IsNull(exc);
        }

        [Test]
        public void AddListMessagesTest()
        {
            Exception exc = null;
            try
            {
                Appuser user = GetTestUser();
                AppuserDAO.InsertAppuser(user);
                Guid guid = Guid.NewGuid();
                List<Message> messages = new List<Message>();
                Message message = GetTestMessage();
                message.Caption = guid.ToString();
                message.Senderid = user.Userid;
                message.Receiverid = user.Userid;
                messages.Add(message);
                message = GetTestMessage();
                message.Caption = guid.ToString();
                message.Senderid = user.Userid;
                message.Receiverid = user.Userid;
                messages.Add(message);
                message = GetTestMessage();
                message.Caption = guid.ToString();
                message.Senderid = user.Userid;
                message.Receiverid = user.Userid;
                messages.Add(message);

                MessageDAO.InsertListMessage(messages);
                List<long> ids = MessageDAO.GetMessageIDs(String.Format(" WHERE CAPTION = '{0}'", guid));

                Assert.AreEqual(messages.Count, ids.Count);
                MessageDAO.DeleteListMessage(messages);
                ids = MessageDAO.GetMessageIDs(String.Format(" WHERE CAPTION = '{0}'", guid));
                Assert.AreEqual(ids.Count, 0);
                AppuserDAO.DeleteAppuser(user);
            }
            catch (Exception ex)
            {
                Trace.Write(ex);
                exc = ex;
            }
            Assert.IsNull(exc);    
        }

        [Test]
        public void UpdateMessageTest()
        {
            Exception exc = null;
            Message message = null;
            try
            {
                message = GetTestMessage();
                Appuser user = GetTestUser();
                AppuserDAO.InsertAppuser(user);
                message.Senderid = user.Userid;
                message.Receiverid = user.Userid;
                MessageDAO.InsertMessage(message);
                Assert.IsNotNull(message.Messageid);

                message.Caption = "test caption ggg";
                message.Messagestatusid = Message.MessageStatus.Delivered;
                message.Messagetypeid = Message.MessageType.Forum;

                MessageDAO.UpdateMessage(message);
                Message message2 = MessageDAO.LoadMessages(String.Format(" WHERE MESSAGEID = {0} ", message.Messageid), "")[0];

                Assert.AreEqual(message.Caption, message2.Caption);
                Assert.AreEqual(message.Messagestatusid, message2.Messagestatusid);
                Assert.AreEqual(message.Messagetypeid, message2.Messagetypeid);

                MessageDAO.DeleteMessage(message);
                AppuserDAO.DeleteAppuser(user);
            }
            catch (Exception ex)
            {
                Trace.Write(ex);
                exc = ex;
            }
            Assert.IsNotNull(message);
            Assert.IsNull(exc);
        }

        [Test]
        public void DeleteMessageTest()
        {
            Exception exc = null;
            Message message = null;
            try
            {
                message = GetTestMessage();
                Appuser user = GetTestUser();
                AppuserDAO.InsertAppuser(user);
                message.Senderid = user.Userid;
                message.Receiverid = user.Userid;
                MessageDAO.InsertMessage(message);
                MessageDAO.DeleteMessage(message);
                List<long> ids = MessageDAO.GetMessageIDs(String.Format(" WHERE MESSAGEID = {0} ", message.Messageid));
                Assert.AreEqual(ids.Count, 0);
                AppuserDAO.DeleteAppuser(user);
            }
            catch (Exception ex)
            {
                Trace.Write(ex);
                exc = ex;
            }
        }
    }
}
